﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Input;
using YanShu.Utils;

namespace YanShu
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static MainWindow current;
        private static Hashtable pages = new Hashtable();
        public MainWindow()
        {
            InitializeComponent();
            current = this;
            this.Loaded += MainWindow_Loaded;
            this.Closing += MainWindow_Closing;
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            //this.Visibility=Visibility.Hidden;
            this.Hide();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            SwtichToLogin();
        }



        /*        private void btnRecClear_Click(object sender, RoutedEventArgs e)
                {
                    txbReceive.Text = String.Empty;
                }*/

        public static void SetText(string msg)
        {
            /* if (current == null) return;
             current.Dispatcher.BeginInvoke(new Action(() =>
             {
                 DateTime now = DateTime.Now;

                 current.txbReceive.AppendText("["+now.ToString("yyyy-MM-dd HH:mm:ss") +"]"+msg + '\n');
             }));*/
        }

        public static void SwtichToLogin() {
            if (pages.ContainsKey("login"))
            {
               current.Content = pages["login"];
            }
            else
            {
                Login login = new Login();
                pages.Add("login", login);
                current.Content = login;
            }
        }

        public static void SwtichToIndex()
        {
            if (pages.ContainsKey("index"))
            {
                current.Content = pages["index"];
            }
            else
            {
                String clientIp = CommonUtils.ReadConfig("clientIp");
                Index index = new Index();
                index.clientIp.Text = clientIp;
                pages.Add("index", index);
                current.Content = index;
            }
        }

       

        private void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PageDragMove();
        }

        public static void PageDragMove() {
            current.DragMove();
        }
        /// <summary>
        /// 隐藏主窗口
        /// </summary>
        public static void HideWindow() {
            current.Hide();
        }
    }
}
